package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.utils.pinyinUtilis.ContactSortModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/2.
 */

public class SortAdapter extends RecyclerView.Adapter<SortAdapter.MyViewHolder>  {
    private List<ContactSortModel> listBean = null;
    private Context context;

    public void setListBean(List<ContactSortModel> listBean){
        this.listBean = listBean;
        notifyDataSetChanged();
    }

    public SortAdapter(Context context){
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contact, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        final ContactSortModel mContent = listBean.get(position);
//        int section = getSectionForPosition(position);
//
//        if (position == getPositionForSection(section)) {
//            holder.tv_catagory.setVisibility(View.VISIBLE);
            holder.tv_catagory.setText(mContent.getSortLetters());
//        } else {
//            holder.tv_catagory.setVisibility(View.GONE);
//        }

        holder.tv_city_name.setText(mContent.getName());
    }

    @Override
    public int getItemCount() {
        return listBean == null ? 0 : listBean.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_catagory)
        TextView tv_catagory;
        @BindView(R.id.tv_city_name)
        TextView tv_city_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public int getCount(){
        return this.listBean.size();
    }

    public int getSectionForPosition(int position) {
        return listBean.get(position).getSortLetters().charAt(0);
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = listBean.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }


}
